import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Server extends JFrame {

	private ServerSocket serverSocket;
	private int port = 6789;
	private int width;
	private int height;
	private volatile Integer rLC = new Integer(0);
	private volatile Integer sLC = new Integer(0);
	private volatile BufferedImage bufferedImage;
	private volatile Graphics2D g;
	private JPanel centerPanel;
	canvas writeimg = new canvas();

	public Server() {

		this.width = 700;
		this.height = 700;
		bufferedImage = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		g = bufferedImage.createGraphics();

		this.setContentPane(writeimg);
		this.setVisible(true);
		this.setBounds(10, 10, 700, 700);
		waitClients();
	}

	private void waitClients() {
		System.out.println("Server started");
		long t = System.currentTimeMillis();

		try {
			serverSocket = new ServerSocket(port);
			while (rLC < height - 1) {
				Socket socket = serverSocket.accept();
				ConnectionWithClient client = new ConnectionWithClient(g,
						height, width, socket, this, writeimg);

			}
			serverSocket.close();
		} catch (IOException e) {
			System.out.println("Hiba:" + e);
		}

	}

	public synchronized void setRenderedLinesCount(Integer renderedLinesCount) {
		this.rLC = renderedLinesCount;
	}

	public synchronized Integer getRenderedLinesCount() {
		return rLC;
	}

	public synchronized void setSendedLinesCount(Integer sendedLinesCount) {
		this.sLC = sendedLinesCount;
	}

	public synchronized Integer getSendedLinesCount() {
		return sLC;
	}
/*
	public synchronized void setGrapicsXY(Color rgb, int x, int y) {
		g.setColor(rgb);
		g.fillRect(x, y, 1, 1);
	}
*/
	public static void main(String[] args) {
		new Server();
	}
}
